package cn.hsa.xxx.approve.service.impl;

import cn.hsa.hsaf.core.mq.MQConsumer;
import cn.hsa.xxx.bookData.bo.BookCommodityBO;
import cn.hsa.xxx.bookData.bo.BookInfoBO;
import cn.hsa.xxx.bookData.bo.BookInventoryBO;
import cn.hsa.xxx.bookData.bo.ShelvesApprovalProcessBO;
import cn.hsa.xxx.api.dto.BookCommodityDTO;
import cn.hsa.xxx.api.dto.BookInfoDTO;
import cn.hsa.xxx.api.dto.BookInventoryDTO;
import cn.hsa.xxx.api.dto.ShelvesApprovalProcessDTO;
import cn.hsa.xxx.approve.service.ConsumerService;
import javafx.util.Pair;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayDeque;
import java.util.Date;
import java.util.Queue;


@Service
@Slf4j
public class ConsumerServiceImpl implements ConsumerService {

    //存放的待审批消息的队列
    private Queue<ShelvesApprovalProcessDTO> approvalQueue = new ArrayDeque<>();

    @Value("${mq.topic.dto}")
    private String dtoTopic;

    @Autowired
    private MQConsumer mqConsumer;

    @Autowired
    private MqDtoConsumerServiceImpl mqDtoConsumerService;

    @Autowired
    private ShelvesApprovalProcessBO shelvesApprovalProcessBO;

    @Autowired
    private BookInventoryBO bookInventoryBO;

    @Autowired
    private BookCommodityBO bookCommodityBO;

    @Autowired
    private BookInfoBO bookInfoBO;


    /**
     *
     * @return
     */
    @Override
    public Queue<ShelvesApprovalProcessDTO> getQueue() {
        mqConsumer.subscribe(dtoTopic, mqDtoConsumerService);
        Queue<ShelvesApprovalProcessDTO> dtoQueue = mqDtoConsumerService.getDtoQueue();

        //队列拼接
        while(!dtoQueue.isEmpty()) {
            approvalQueue.add(dtoQueue.peek());
            dtoQueue.remove();
        }

        return approvalQueue;
    }

    /**
     * flag为false， 审核不通过，修改审核流程表
     * flag为true，审核通过，修改审核流程表、库存表和商品表
     * @param flag 审核状态
     * @return
     */
    @Override
    @Transactional//开启事务
    public Pair<String, Boolean> addApproveDtoList(boolean flag) throws Exception {
        if(approvalQueue == null) {
            return new Pair("-1", false);
        }

        ShelvesApprovalProcessDTO shelvesApprovalProcessDTO = approvalQueue.peek();
        log.debug("shelvesApprovalProcessDTO:{}", shelvesApprovalProcessDTO);
        boolean approvalFlag = false;
        if(flag == false) {//审核不通过
            approvalFlag = false;
            //存入上架审批表
            //审批状态
            shelvesApprovalProcessDTO.setApprovalType(new Date(0));
            //审批时间
            Date date = new Date();
            shelvesApprovalProcessDTO.setEndApprovalTime(date);
            shelvesApprovalProcessDTO.setUpdateTime(date);
            shelvesApprovalProcessBO.add(shelvesApprovalProcessDTO);

        }else {
            approvalFlag = true;
            //存入上架审批表
            //审批状态
            shelvesApprovalProcessDTO.setApprovalType(new Date(1));
            //审批时间
            Date date = new Date();
            shelvesApprovalProcessDTO.setEndApprovalTime(date);
            shelvesApprovalProcessDTO.setUpdateTime(date);
            //
            shelvesApprovalProcessBO.add(shelvesApprovalProcessDTO);

            String bookUuid = shelvesApprovalProcessDTO.getBookUuid();
            //改变库存表
            //查找库存表
            BookInventoryDTO bookInventoryDTO = bookInventoryBO.getByBookUuid(bookUuid);
            if(bookInventoryDTO == null || bookInventoryDTO.getInventoryRemaining() == 0) {
                throw new Exception("没有该库存或者库存为0");
            }
            //更改库存剩余数量
            int inventoryRemaining = bookInventoryDTO.getInventoryRemaining();
            int quantity = shelvesApprovalProcessDTO.getQuantity();
            bookInventoryDTO.setInventoryRemaining(inventoryRemaining-quantity);
            //更改库存数更新时间和库存更新时间
            bookInventoryDTO.setInventoryQuantityUpdateTime(date);
            bookInventoryDTO.setUpdateTime(date);
            //更新
            bookInventoryBO.update(bookInventoryDTO);

            //一些bookCommodity的属性
            Float price = shelvesApprovalProcessDTO.getUnitPrice();
            BookInfoDTO bookInfoDTO = bookInfoBO.get(bookUuid);
            String description = bookInfoDTO.getProfiles();
            //改变商品表
            //查找商品表，有则更新，无则创建
            BookCommodityDTO bookCommodityDTO = bookCommodityBO.getByBookUuid(bookUuid);
            if(bookCommodityDTO == null) {//创建
                bookCommodityBO.add(new BookCommodityDTO(bookUuid, bookUuid, price, quantity, quantity, "1", description, date, date));
            }else {//更新
                bookCommodityDTO.setCommodityPrice(price);
                int bookCommodityQuantity = bookCommodityDTO.getQuantity() + quantity;
                bookCommodityDTO.setQuantity(bookCommodityQuantity);
                int bookCommodityRemainingQuantity = bookCommodityDTO.getShelvesRemainingQuantity() + quantity;
                bookCommodityDTO.setShelvesRemainingQuantity(bookCommodityRemainingQuantity);
                bookCommodityDTO.setUpdateTime(date);
                bookCommodityBO.update(bookCommodityDTO);
            }

        }
        //出队列
        approvalQueue.remove();

        return new Pair(shelvesApprovalProcessDTO.getBookUuid(), approvalFlag);
    }

}
